package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.keys.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService detailService;

    @Reference
    private IStrategyTagService tagService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCommentService commentService;

    @Reference
    private IStrategyRedisService strategyRedisService;

    @Reference
    private IStrategyCommendService commendService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @RequestMapping("")
    public String index(Model model) {
        //commends
        model.addAttribute("commends", commendService.getTop5());
        List<StrategyStatisVO> list = strategyRedisService.getAllZsetVo(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(), 0, -1);
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO statisVO : list) {
            if (statisVO.isIsabroad()) {
                if (abroadCds.size() < 10) {
                    abroadCds.add(statisVO);
                }
            } else {
                if(unabroadCds.size() < 10){
                    unabroadCds.add(statisVO);
                }
            }
        }
        //abroadCds
        model.addAttribute("abroadCds",abroadCds);
        //unabroadCds
        model.addAttribute("unabroadCds",unabroadCds);
        //hotCds
        List<StrategyStatisVO> hotCds = strategyRedisService.getAllZsetVo(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),0,9);
        model.addAttribute("hotCds",hotCds);
        //themes
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionGroup(StrategySearchQuery.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);
        //abroads
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionGroup(StrategySearchQuery.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);
        //chinas
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionGroup(StrategySearchQuery.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas",chinas);
        model.addAttribute("themeCds",strategyTemplateSearchService.getThemeCommend());

        return "strategy/index";
    }

    @RequestMapping("searchPage")
    public String searchPage(@ModelAttribute("qo") StrategySearchQuery qo,Model model){
        model.addAttribute("page",strategyTemplateSearchService.query(qo));
        return "strategy/searchPageTpl";
    }


    @RequestMapping("detail")
    public String detail(Long id, Model model, @UserParam UserInfo userInfo) {
        model.addAttribute("detail", detailService.get(id));
        strategyRedisService.viewNumIncrease(id, 1);
        model.addAttribute("vo", strategyRedisService.getStatisVo(id));
        //isFavor
        if (userInfo != null) {
            model.addAttribute("isFavor", strategyRedisService.isFavor(id, userInfo.getId()));
        }
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix()
                , id, 1);
        return "strategy/detail";
    }

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //tags
        List<StrategyTag> tags = tagService.list();
        model.addAttribute("tags", tags);
        PageInfo pageInfo = detailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        Destination dest = destinationService.getById(qo.getDestId());
        model.addAttribute("dest", dest);
        List<Destination> toasts = destinationService.getToasts(dest.getParent().getId());
        model.addAttribute("toasts", toasts);
        return "strategy/list";
    }

    @RequestMapping("commentAdd")
    @ResponseBody
    @RequireLogin
    public AJaxResult commentAdd(StrategyComment comment, @UserParam UserInfo userInfo) {
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        commentService.addComment(comment);
        strategyRedisService.replyNumIncrease(comment.getDetailId(), 1);
        StrategyStatisVO statisVo = strategyRedisService.getStatisVo(comment.getDetailId());
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix()
                , comment.getDetailId(), 1);
        return AJaxResult.SUCCESS().addData(statisVo.getReplynum());
    }

    @RequestMapping("comment")
    public String comment(@ModelAttribute("qo") StrategyCommentQuery qo, Model model, @UserParam UserInfo userInfo) {
        Page<StrategyComment> page = commentService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("userInfo", userInfo);
        return "strategy/commentTpl";
    }


    @RequestMapping("commentThumbUp")
    @ResponseBody
    @RequireLogin
    public AJaxResult commentThumbUp(String toid, Long fromid) {
        StrategyComment comment = commentService.commentThumbUp(toid, fromid);
        return AJaxResult.SUCCESS().addData(comment);
    }

    @RequestMapping("favor")
    @ResponseBody
    public AJaxResult favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AJaxResult aJaxResult = new AJaxResult(false, "请登录后再收藏!");
            aJaxResult.setCode(102);
            return aJaxResult;
        }
        boolean ret = strategyRedisService.favor(sid, userInfo.getId());
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix()
                , sid, ret ? 1 : -1);
        if(ret){
            detailService.saveFavorToUser(sid, userInfo.getId ());
        }else{
            detailService.deleteUserFavor(userInfo.getId (), sid);
        }
        return new AJaxResult(ret, "").addData(strategyRedisService.getStatisVo(sid));
    }

    @RequestMapping("strategyThumbup")
    @ResponseBody
    public AJaxResult strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AJaxResult aJaxResult = new AJaxResult(false, "请登录后再顶!");
            aJaxResult.setCode(102);
            return aJaxResult;
        }
        boolean ret = strategyRedisService.strategyThumbup(sid, userInfo.getId());
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix()
                , sid, ret ? 1 : 0);
        return new AJaxResult(ret, "").addData(strategyRedisService.getStatisVo(sid));
    }

    @RequestMapping("share")
    @ResponseBody
    public AJaxResult share(Long sid) {
        strategyRedisService.strategyShare(sid, 1);
        return AJaxResult.SUCCESS().addData(strategyRedisService.getStatisVo(sid));
    }
}
